package com.xl.rentkeeper.act.jilu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.ViewOrderStatus;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.BaseAct;
import com.xl.rentkeeper.adapter.OrderAdapter;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.view.TitleBar;

import java.util.List;

/**
 * Created by Administrator on 2015/8/17.
 */
public abstract class BaseOrderAct extends BaseAct implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, AbsListView.OnScrollListener {

    private ListView mLVOrder;
    private OrderAdapter mAdapter;
    private List<Order> mList;
    private SwipeRefreshLayout mSRLList;
    private View mFooterView;
    private boolean isLoadFinish = false;
    private int visibleItemCount,visibleLastIndex;
    private TextView mTVNodata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_order);
        init();
    }
    private void init(){
        TitleBar tb = (TitleBar) findViewById(R.id.tb_order);
        mSRLList =  (SwipeRefreshLayout)findViewById(R.id.id_swipe_ly);
        mSRLList.setOnRefreshListener(this);
        mTVNodata = (TextView)findViewById(R.id.tv_nodata);
        initTitleBar(tb);
        setTranslucentStatus();
        tb.setTitleName(getTitleName());
        mLVOrder = (ListView) findViewById(R.id.lv_order);
        mLVOrder.setOnScrollListener(this);
        mFooterView = getLayoutInflater().inflate(R.layout.load_more,null);
        mList = OrderLogic.getInstance().getHistoryOrderListByStatus(getPBStatus(),true);
        if(mList.size()!=0){
            mLVOrder.addFooterView(mFooterView);
            requestPro();
        }else{
            requestNext();
        }
//        mAdapter = new OrderAdapter(getLayoutInflater(),mList);
        mAdapter = new OrderAdapter(this,getLayoutInflater(),mList);
        mLVOrder.setAdapter(mAdapter);
        mLVOrder.setOnItemClickListener(this);
    }
    public abstract String getTitleName();
    public abstract String getStatus();
    public int getPBStatus(){
        return Integer.parseInt(OrderLogic.statusMap.get(getStatus()));
    }
    public void refreshView(){
        mList.clear();
        mList.addAll(OrderLogic.getInstance().getHistoryOrderListByStatus(getPBStatus(),true));
        if(mList.size()==0){
            mTVNodata.setVisibility(View.VISIBLE);
        }else{
            mTVNodata.setVisibility(View.INVISIBLE);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public String[] monitorEvents() {
        return new String[]{getStatus()};
    }
    public void requestNext(){
        long[] minmaxTime = OrderLogic.getInstance().getMinMaxUpDateTime(getPBStatus());
        OrderLogic.getInstance().queryOrder(0, getStatus(), minmaxTime[0], 0);
    }
    public void requestPro(){
        long[] minmaxTime = OrderLogic.getInstance().getMinMaxUpDateTime(getPBStatus());
        OrderLogic.getInstance().queryOrder(0, getStatus(),0 ,minmaxTime[1]);
    }

    @Override
    public void onRefresh() {
        requestPro();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        QLog.d("aa","onScrollStateChanged");
        int itemsLastIndex = mAdapter.getCount() - 1;    //数据集最后一项的索引
        int lastIndex = itemsLastIndex + 1;             //加上底部的loadMoreView项
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
            //如果是自动加载,可以在这里放置异步加载数据的代码
            requestNext();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }


    public class OrderInfo{
        public OrderInfo(String state,String backup){
            this.state = state;
            this.backup = backup;
        }
        public String state;
        private String backup;
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        super.onEvent(event, ret,errInfo, data);
        if (event.equals(getStatus())){
            mSRLList.setRefreshing(false);
            if(ret){
                isLoadFinish = (Boolean)data[0];
                refreshView();
                if(isLoadFinish){
                    if(mLVOrder.getFooterViewsCount()>0){
                        mLVOrder.removeFooterView(mFooterView);
                    }
                }else{
                    if(mLVOrder.getFooterViewsCount()==0){
                        mLVOrder.addFooterView(mFooterView);
                    }
                }
            }else{
                App.showToast(errInfo);
                if(mLVOrder.getFooterViewsCount()>0){
                    mLVOrder.removeFooterView(mFooterView);
                }
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position>=0 && position<mList.size()) {
            int state = mList.get(position).viewOrder.status;
           if (state == ViewOrderStatus.ORDER_IS_SIGN.getValue()){
               Intent intent = new Intent(this, SignedDetailAct.class);
               intent.putExtra("title",getTitleName());
               intent.putExtra("msg",mList.get(position));
               startActivity(intent);
               overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
           }else if (state == ViewOrderStatus.ORDER_NOT_SIGN.getValue()){
               Intent intent = new Intent(this, NotSignDetailAct.class);
               intent.putExtra("title",getTitleName());
               intent.putExtra("msg",mList.get(position));
               startActivity(intent);
               overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);

           }else if (state == ViewOrderStatus.ORDER_NOT_VIEW.getValue()){
               Intent intent = new Intent(this, NotSignDetailAct.class);
               intent.putExtra("title",getTitleName());
               intent.putExtra("msg",mList.get(position));
               startActivity(intent);
               overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
           }

        }
    }
}
